package com.zbkj.admin.controller.platform.brand;

import com.zbkj.common.annotation.LogControllerAnnotation;
import com.zbkj.common.enums.MethodType;
import com.zbkj.common.model.admin.SystemRole;
import com.zbkj.common.model.brand.Brand;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.SystemRoleSearchRequest;
import com.zbkj.common.request.brand.*;
import com.zbkj.common.response.*;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.BrandService;
import com.zbkj.service.service.SystemRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 品牌开户控制器
 +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/platform/brand")
@Api(tags = "品牌开户控制器")
public class PlatformBrandController {

    @Autowired
    private BrandService brandService;
    @Autowired
    private SystemRoleService systemRoleService;


    @ApiOperation(value="品牌分页列表")
    @PreAuthorize("hasAuthority('platform:brand:page:list')")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<BrandPageResponse>> getPageList(@Validated BrandRequest searchRequest,
                                                                   @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(brandService.getAdminPage(searchRequest, pageParamRequest)));
    }


    @ApiOperation(value="品牌分页列表表头数量")
    @PreAuthorize("hasAuthority('platform:brand:list:header:num')")
    @RequestMapping(value = "/list/header/num", method = RequestMethod.GET)
    public CommonResult<BrandHeaderNumResponse> getListHeaderNum(@Validated BrandRequest searchRequest) {
        return CommonResult.success(brandService.getListHeaderNum(searchRequest));
    }

    @LogControllerAnnotation(intoDB = true, methodType = MethodType.ADD, description = "添加品牌")
    @ApiOperation(value="添加品牌")
    @PreAuthorize("hasAuthority('platform:brand:add')")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult<String> add(@RequestBody @Validated BrandAddRequest request) {
        if (brandService.add(request)) {
            if (request.getIsSwitch()) {
                // 刷新品牌商缓存
                brandService.cacheBrandInfo();
            }
            return CommonResult.success("添加商户成功");
        }
        return CommonResult.failed("添加商户失败");
    }

    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "编辑品牌")
    @ApiOperation(value="编辑品牌")
    @PreAuthorize("hasAuthority('platform:brand:update')")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated BrandUpdateRequest request) {
        if (brandService.edit(request)) {
            brandService.cacheBrandInfo();
            return CommonResult.success("编辑品牌成功");
        }
        return CommonResult.failed("编辑品牌失败");
    }

    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "修改品牌手机号")
    @PreAuthorize("hasAuthority('platform:brand:update:phone')")
    @ApiOperation(value="修改品牌手机号")
    @RequestMapping(value = "/update/phone", method = RequestMethod.POST)
    public CommonResult<String> updatePhone(@RequestBody @Validated BrandUpdatePhoneRequest request) {
        if (brandService.updatePhonePlarform(request)) {
            return CommonResult.success("修改品牌手机号成功");
        }
        return CommonResult.failed("修改品牌手机号失败");
    }

    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "重置品牌密码")
    @PreAuthorize("hasAuthority('platform:brand:reset:password')")
    @ApiOperation(value="重置品牌密码")
    @RequestMapping(value = "/reset/password/{id}", method = RequestMethod.POST)
    public CommonResult<String> resetPassword(@PathVariable(value = "id") Integer id) {
        if (brandService.resetPassword(id)) {
            return CommonResult.success("重置品牌密码成功");
        }
        return CommonResult.failed("重置品牌密码失败");
    }

/*    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "修改复制品牌数量")
    @PreAuthorize("hasAuthority('platform:brand:copy:prodcut:num')")
    @ApiOperation(value="修改复制品牌数量")
    @RequestMapping(value = "/update/copy/product/num", method = RequestMethod.POST)
    public CommonResult<String> updateCopyProductNum(@RequestBody @Validated MerchantUpdateProductNumRequest request) {
        if (brandService.updateCopyProductNum(request)) {
            return CommonResult.success("修改复制品牌数量成功");
        }
        return CommonResult.failed("修改复制品牌数量失败");
    }*/
    @PreAuthorize("hasAuthority('platform:brand:detail')")
    @ApiOperation(value="品牌详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public CommonResult<BrandDetailResponse> getDetail(@PathVariable("id") Integer id) {
        return CommonResult.success(brandService.getPlatformDetail(id));
    }

   /* @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "推荐开关")
    @PreAuthorize("hasAuthority('platform:brand:recommend:switch')")
    @ApiOperation(value="推荐开关")
    @RequestMapping(value = "/recommend/switch/{id}", method = RequestMethod.POST)
    public CommonResult<String> recommendSwitch(@PathVariable("id") Integer id) {
        if (brandService.recommendSwitch(id)) {
            return CommonResult.success("切换品牌推荐开关成功");
        }
        return CommonResult.failed("切换品牌推荐开关失败");
    }*/

    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "关闭品牌")
    @PreAuthorize("hasAuthority('platform:brand:close')")
    @ApiOperation(value="关闭品牌")
    @RequestMapping(value = "/close/{id}", method = RequestMethod.POST)
    public CommonResult<String> close(@PathVariable("id") Integer id) {
        if (brandService.close(id)) {
            brandService.cacheBrandInfo();
            return CommonResult.success("关闭品牌成功");
        }
        return CommonResult.failed("关闭品牌失败");
    }

    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "开启品牌")
    @PreAuthorize("hasAuthority('platform:brand:open')")
    @ApiOperation(value="开启品牌")
    @RequestMapping(value = "/open/{id}", method = RequestMethod.POST)
    public CommonResult<String> open(@PathVariable("id") Integer id) {
        if (brandService.open(id)) {
            brandService.cacheBrandInfo();
            return CommonResult.success("开启品牌成功");
        }
        return CommonResult.failed("开启品牌失败");
    }


    @ApiOperation(value = "角色分页列表")
    @RequestMapping(value = "/roleList", method = RequestMethod.GET)
    public CommonResult<CommonPage<SystemRole>> getList(@Validated SystemRoleSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<SystemRole> systemRoleCommonPage = CommonPage.restPage(systemRoleService.getList(request, pageParamRequest));
        return CommonResult.success(systemRoleCommonPage);
    }
 /*   @PreAuthorize("hasAuthority('platform:brand:use:category:list')")
    @ApiOperation(value="可用分类品牌列表")
    @RequestMapping(value = "/use/category/list", method = RequestMethod.GET)
    public CommonResult<List<CategoryMerchantResponse>> getUseCategoryList() {
        return CommonResult.success(brandService.getUseCategoryList());
    }*/
    @ApiOperation(value = "推广链接")
    @PreAuthorize("hasAuthority('platform:brand:link')")
    @RequestMapping(value = "/brandLink", method = RequestMethod.GET)
    public CommonResult<CommonPage<BrandLinkResponse>> brandLink(@Validated BrandLink brandLink, @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(brandService.brandLink(brandLink, pageParamRequest)));
    }

}
